﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SnmpForm
{
    public partial class FrmSystemLog : FrmSub
    {
        public FrmSystemLog(MainForm objForm)
            :base(objForm)
        {
            InitializeComponent();
        }

        static public SelectItem[] 
            LOG_LEVEL = { new SelectItem("全部", DbWrapper.DbDataTypes.LogInfo.LEVEL.UNKNOWN), 
                                    new SelectItem("信息", DbWrapper.DbDataTypes.LogInfo.LEVEL.INFO), 
                                    new SelectItem("告警", DbWrapper.DbDataTypes.LogInfo.LEVEL.WARMING),
                                    new SelectItem("错误", DbWrapper.DbDataTypes.LogInfo.LEVEL.ERROR)};

        static public SelectItem[]
            LOG_TYPE = { new SelectItem("全部", DbWrapper.DbDataTypes.LogInfo.TYPE.UNKNOWN), 
                                    new SelectItem("系统", DbWrapper.DbDataTypes.LogInfo.TYPE.SYSTEM), 
                                    new SelectItem("用户", DbWrapper.DbDataTypes.LogInfo.TYPE.USER),
                                    new SelectItem("AP", DbWrapper.DbDataTypes.LogInfo.TYPE.AP)};

        private Dictionary<int, string> mapUsername = 
            new Dictionary<int, string>();
        private void FrmSystemLog_Load(object sender, EventArgs e)
        {
            InitSelectCondition();

            dataGridViewSyslog.ColumnCount = 6;
            dataGridViewSyslog.Columns[0].Name = "编号";
            dataGridViewSyslog.Columns[1].Name = "用户";
            dataGridViewSyslog.Columns[2].Name = "级别";
            dataGridViewSyslog.Columns[3].Name = "类型";
            dataGridViewSyslog.Columns[4].Name = "信息";
            dataGridViewSyslog.Columns[5].Name = "发生时间";
        }

        private void InitSelectCondition()
        {
            comboBoxLevel.DataSource = LOG_LEVEL.Clone();
            comboBoxType.DataSource = LOG_TYPE.Clone();
            //从数据库中读取有权限查询的用户
            List<SelectItem> WaitSelectUser = new List<SelectItem>();
            WaitSelectUser.Add( new SelectItem("全部", DbWrapper.DbDataTypes.User.UNKNOW));
            mapUsername[DbWrapper.DbDataTypes.User.UNKNOW] = "系统";

            List<DbWrapper.DbDataTypes.User> listUser =
                BusinessWrapper.User.Instance.GetUserList();
            if (listUser!=null)
            {
                foreach (DbWrapper.DbDataTypes.User user in listUser)
                {
                    SelectItem item = new SelectItem(user.username, user.id);
                    WaitSelectUser.Add(item);

                    mapUsername[user.id] = user.username;
                }
            }

            comboBoxUser.DataSource = WaitSelectUser;

            //开始时间，当天凌晨开始
            DateTime timeToday = dateTimePickerStart.Value;
            dateTimePickerStart.Value =
                new DateTime(timeToday.Year,
                    timeToday.Month,
                    timeToday.Day);
        }

        private void buttonSelect_Click(object sender, EventArgs e)
        {
            
            string strUsername = "";

            SelectItem item = null;
            try
            {
                item = (SelectItem)comboBoxUser.SelectedItem;
                if (item != null)
                {
                    int nUserId = (int)item.Value;
                    if (nUserId == DbWrapper.DbDataTypes.User.UNKNOW)
                    {
                        strUsername = "";
                    }
                    else
                    {
                        strUsername = comboBoxUser.Text;
                    }
                }
                else
                {
                    strUsername = comboBoxUser.Text; 
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                strUsername = comboBoxUser.Text;
            }

            item = (SelectItem)comboBoxLevel.SelectedItem;
            int nLevel = (int)item.Value;

            item = (SelectItem)comboBoxType.SelectedItem;
            int nType = (int)item.Value;

            DateTime timeStart = dateTimePickerStart.Value;
            DateTime timeEnd = dateTimePickerEnd.Value;

            List<DbWrapper.DbDataTypes.LogInfo> list =
                BusinessWrapper.User.Instance.SelectLog(
                    strUsername,
                    nLevel,
                    nType,
                    timeStart,
                    timeEnd);

            DataGridViewRowCollection rows = dataGridViewSyslog.Rows;
            rows.Clear();
            if (list != null)
            {
                foreach (DbWrapper.DbDataTypes.LogInfo info in list)
                {
                    string strId = info.id.ToString();
                    bool bExist =
                        mapUsername.TryGetValue(info.userid, out strUsername);
                    if (!bExist)
                    {
                        strUsername = "未知";
                    }
                    string strLevel = LOG_LEVEL[info.level].Text;
                    string strType = LOG_TYPE[info.logtype].Text;
                    string strLoginfo = info.loginfo;
                    string strDatetime = info.datetime.ToString();
                    string[] row = { strId, 
                                   strUsername, 
                                   strLevel, 
                                   strType, 
                                   strLoginfo, 
                                   strDatetime};
                    rows.Add(row);

                }
            }
        }
    }
    public class SelectItem : APConfig.ApConfigItem
    {
        public SelectItem(string text, object value)
            : base(text, value)
        {
        }
    }
}
